﻿/// <reference path="RequestHandler.js" />
/// <reference path="../HttpRequest.js" />
/// <reference path="../../enhancements/QuickEdit.js" />
/// <reference path="../../enhancements/QuickReply.js" />
/// <reference path="../../enhancements/QuickQuote.js" />
/// <reference path="../../enhancements/markup-strip/MarkupStripper.js" />
/// <reference path="../../enhancements/markup-strip/selectors/ViewThreadMarkupElementSelector.js" />
/// <reference path="../../utilities/Post.js" />
/// <reference path="../../utilities/Xpcom.js" />
///--------------------------------------------------------------------
/// <file name="ViewThreadHandler.js">
///   Adds quick reply, quick edit, quick quote,
///   and other features to the view thread page.
/// </file>
///--------------------------------------------------------------------

NAMFox.ns(function() { with (NAMFox) {

this.ViewThreadHandler = function() {
};

ViewThreadHandler.prototype = new RequestHandler();
ViewThreadHandler.prototype.name = "ViewThreadHandler";

ViewThreadHandler.prototype.handleRequestCore = function(request, _$) {
    /// <summary>Handles a request.</summary>
    /// <param name="request" type="HttpRequest">
    ///   The HTTP request which encapsulates the document
    ///   and route data for this request.
    /// </param>

    function htmlLoaded() {
        try {
            var serverHtml = this.contentDocument.body.textContent;

            // When the thread does not have a title this regex will not match.
            var subject = _$("#current").html();
    
            var thread = new Thread(
                _$,
                request.routeData.forumId,
                request.routeData.threadId,
                subject,
                serverHtml
            );
    
            _$("head").append(
                '<link id="namfox-view-thread-css" href="chrome://namfox/skin/view-thread.css" rel="stylesheet" type="text/css" />'
            );

            var markupStripper = new MarkupStripper(new ViewThreadMarkupElementSelector(thread));
            var quickReply = thread.quickReply = new QuickReply(_$, request.htmlDocument, thread);
            var quickEdit = thread.quickEdit = new QuickEdit(_$, request.htmlDocument, thread);
            var quickQuote = thread.quickQuote = new QuickQuote(_$, request.htmlDocument, thread);
            var quickMove = thread.quickMove = new QuickMove(_$, request.htmlDocument, thread);
        
            markupStripper.attach();
            quickReply.attach();
            quickEdit.attach();
            quickQuote.attach();
            quickMove.attach();
        
            request.htmlDocument.__namfox__.textArea = _$("#namfox-quick-reply-textarea");
        }
        catch (e) {
            $.error(e);
        }
    }

    var iframe = request.htmlDocument.createElement("iframe");
    iframe.id = "namfox-view-source-frame";
    iframe.src = "view-source:" + request.htmlDocument.location.href;
    iframe.addEventListener(
        "load",
        htmlLoaded,
        false);
    request.htmlDocument.body.appendChild(iframe);
};

}});